import scrapy
from ..items import BookItem
from scrapy.linkextractors import LinkExtractor
import json

class BooksSpider(scrapy.Spider):
    # 爬虫名，每一个爬虫的唯一标识
    name = 'books'
    # 定义爬虫爬取的起始点，起始点可以是多个，这里只有一个。
    #start_urls = ['http://books.toscrape.com/']
    #http://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&secids=1.000001,0.399001&fields=f62&ut=b2884a393a59ad64002292a3e90d46a5
    # 构造请求头信息
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
    }
    url = "http://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&secids=1.000001,0.399001&fields=f62&ut=b2884a393a59ad64002292a3e90d46a5"
    # 实现 start_resquests 方法代替start_urls类属性
    def start_requests(self):
        yield scrapy.Request(url=self.url,
                             callback=self.parse,
                             headers=self.header,
                             dont_filter=True)

    def parse(self, response):
        # 提取数据
        json_data =response.body.decode()
        load_data = json.loads(json_data)
        diff_data = load_data.get("data").get("diff")
        result1 = []

        for i in diff_data:
            result1.append(i.get("f62"))    # 1

        # print(result1)  # [1903191040.0, 3155650816.0]
        money = BookItem()
        money['shang'] = result1[0]
        money['shen']= result1[1]
        yield money
        #yield to pipe line


